package com.dz.risk.aspect;

import com.dz.risk.exception.VerifyException;
import com.dz.risk.pojo.domain.BasicBondInformationEntity;
import com.dz.risk.service.BasicBondInformationService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 债卷基本信息切面处理类
 *
 * @author Doke
 * @date 2021/7/13 16:03
 */
@Component
@Aspect
public class BasicBondInformationAop {

    private final BasicBondInformationService basicBondInformationService;

    @Autowired
    public BasicBondInformationAop(BasicBondInformationService basicBondInformationService) {
        this.basicBondInformationService = basicBondInformationService;
    }

    /**
     * 债卷基础信息验证 updateById
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.dz.risk.service.impl.BasicBondInformationServiceImpl.add(..)) || execution(* com.dz.risk.service.impl.BasicBondInformationServiceImpl.updateById(..))")
    public Object checkBasicBondInformation(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        BasicBondInformationEntity basicBondInformationEntity = (BasicBondInformationEntity) args[0];

        //根据债卷类型验证新增时各类型额外必传的参数(未传时直接抛出异常)
        checkParamsByBondType(basicBondInformationEntity);

        //根据各债卷类型查重(重复时直接抛出异常)
        checkAddDuplicateOrUpdateDuplicate(basicBondInformationEntity);

        return point.proceed();
    }

    /**
     * 验证数据是否重复(如果相同债卷类型的债卷代码相同或者开始时间及结束时间存在已有的数据开始结束区间段之间都不允许在录入)
     *
     * @param basicBondInformationEntity 债卷基本信息实体
     */
    private void checkAddDuplicateOrUpdateDuplicate(BasicBondInformationEntity basicBondInformationEntity) {

        List<BasicBondInformationEntity> basicBondInformationEntities = basicBondInformationService.selectDuplicateByBondCodeOrBeginDateOrEndDateOrId(basicBondInformationEntity);

        if (!CollectionUtils.isEmpty(basicBondInformationEntities)) {
            throw new VerifyException("该类型的债卷代码已存在或者开始时间及结束时间在已有的数据开始结束区间段之间");
        }

    }

    /**
     * 根据债卷类型验证额外必传的参数(此处由于时间原因,暂时违背了开闭原则)
     *
     * @param basicBondInformationEntity 债卷基础信息实体
     * @return 验证结果(true : 通过, false : 失败)
     */
    private void checkParamsByBondType(BasicBondInformationEntity basicBondInformationEntity) {
        //1 : 自有资金承接深分大集合债券 , 2 : 余额包销债券 , 3 : 计财投资证券（债券、同业存单）
        final int two = 2;
        final int three = 3;

        Integer bondType = basicBondInformationEntity.getBondType();

        //新增余额包销债卷类型时包销金额不能为空
        if (two == bondType && Objects.isNull(basicBondInformationEntity.getExclusiveSalesAmount())) {
            throw new VerifyException("包销金额不能为空");

            //新增计财投资证券类型时成本不能为空
        } else if (three == bondType && Objects.isNull(basicBondInformationEntity.getCost())) {
            throw new VerifyException("成本不能为空");
        }

    }
}
